<?php
require_once __DIR__.'/admin/Bootstrap.php';
require __DIR__.'/site2/config/config.inc.php';
require_once __DIR__.'/site2/includes/CartMgr.class.php';
require_once __DIR__.'/site2/includes/WishListMgr.class.php';
require_once(MYSQL);

$cartMgr= new CartMgr($dbc);
$wishlistMgr= new WishListMgr($dbc);

// Check for, or create, a user session:
$uid=$wishlistMgr->getUserSession();
// Send the cookie:
$wishlistMgr->sendCookie($uid);


$_main_content = new Templater(__DIR__.'/site2/views/wishlist/default.html');
// If there's a productid value in the URL, break it down into its parts:
$pid=getFormValue('pid');
$action=getFormValue('action');
$qty=getFormValue('qty');
$quantity=getFormValue('quantity');

if (isset ($pid,$action) && ($action == 'add') ) { 
	// Add a new product to the wishlist:
	$wishlistMgr->addToWishList($uid, $pid, 1);	
		
} elseif (isset ($pid, $action) && ($action == 'remove') ) { 
	// Remove it from the wishlist.	
	$wishlistMgr->removeFromWishList($uid, $pid);
	
} elseif (isset ($pid, $qty, $action) && ($action == 'move') ) { // Move it to the cart.
	// Determine the quantity:
	$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 1))) ? $qty : 1;
	
	// Add it to the wishlist:
	$wishlistMgr->addToWishList($uid, $pid, $qty);	
	
	// Remove it from the wish list:
	$cartMgr->removeFromCart($uid, $pid);
} elseif (isset($quantity)) { // Update quantities in the cart.
	
	// Loop through each item:
	foreach ($quantity as $pid => $qty) {
		
		
		if (isset($pid)) {

			// Determine the quantity:
			$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1;

			// Update the quantity in the wishlist:
			$wishlistMgr->updateWishList($uid, $pid, $qty);

		}
			
	} // End of FOREACH loop.
	
}// End of main IF.

//$id=$_main_content->getFormValue('id',$dbc);
//if(isset($id))
$_main_content->set('wishlist',$wishlistMgr->getShoppingWishListContents($uid));

$_page = new Templater(__DIR__.'/site2/views/page/page.html');
$_page->set("content", $_main_content->parse());
$_page->publish();